home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1981 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Overloading conversion
  5. Date: 14 Jan 1996 19:42:35 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan14204236@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4d934v$10hs@ds2.acs.ucalgary.ca>
  10.     <ENNO.96Jan14111826@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  11.     <30f93747.165768512@nntp.ix.netcom.com>
  12. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  13. In-reply-to: miker3@ix.netcom.com's message of Sun, 14 Jan 1996 17:05:03 GMT
  14.  
  15. In article <30f93747.165768512@nntp.ix.netcom.com> miker3@ix.netcom.com (Mike Rubenstein) writes:
  16.  
  17.    enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) wrote:
  18.  
  19.    |>In article <4d934v$10hs@ds2.acs.ucalgary.ca>
  20.    hdang@acs3.acs.ucalgary.ca (Hanna Dang) writes:
  21.    |>
  22.    |>   Is there a way to overload functions such that certain
  23.    |>   functions are called depending on the context the object are
  24.    |>   used. For example,
  25.    |>
  26.    |>   class T {
  27.    |>     public:
  28.    |>       int& int_value() { modified = TRUE; return value;}
  29.    |>       int int_value() {return value;}
  30.    |>    private:
  31.    |>       int value;
  32.    |>       int modified;
  33.    |>   }
  34.    |>
  35.    |>
  36.    |>   ...
  37.    |>
  38.    |>   T a;
  39.    |>   int test = a + 6 // int int_value should be called;
  40.    |>   a = 20 // int& int_value should be called.
  41.    |>
  42.    |>   However, this does not work because in both case int& int_value
  43.    |>   is called.  Is there another way to find out if value is changed
  44.    |>   or it's just inspected.
  45.    |>
  46.    |>Yes -- make the latter 'int_value' a const member-function, ie.
  47.    |>
  48.    |>        int int_value() const {return value;}
  49.  
  50.    Have you tried this?  If so, what compiler?  I want to make sure I
  51.    avoid it.  
  52.  
  53.    The decision as to whether to call the const or non-const version of a
  54.    member function is based on whether the class object is const, not on
  55.    which side of the equal sign it appears.  In both cases above, the
  56.    non-const version will be called since a is not const.
  57.  
  58. Yep, you are correct -- my previous posting was completly rubbish.
  59.  
  60.         Enno
  61.